package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.object.pojo.AccountDepositBound;
import com.tbit.uqbike.service.business.AccountDepositBoundService;
import com.tbit.uqbike.webmanager.dao.core.AccountDepositBoundDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 区域互通表(AccountDepositBound)表服务实现类
 *
 * @author jing
 * @since 2020-12-30 16:08:58
 */
@Service("AccountDepositBoundService")
public class AccountDepositBoundServiceImpl implements AccountDepositBoundService {
    @Resource
    private AccountDepositBoundDao AccountDepositBoundDao;

    /**
     * 通过ID查询单条数据
     *
     * @param sourceAccountId 主键
     * @param type
     * @return 实例对象
     */
    @Override
    public List<AccountDepositBound> queryById(Integer sourceAccountId, Integer type) {
        return this.AccountDepositBoundDao.queryById(sourceAccountId, type);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<AccountDepositBound> queryAllByLimit(int offset, int limit) {
        return this.AccountDepositBoundDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param AccountDepositBound 实例对象
     * @return 实例对象
     */
    @Override
    public AccountDepositBound insert(AccountDepositBound AccountDepositBound) {
        AccountDepositBoundDao.deleteById(AccountDepositBound.getSourceAccountId(), AccountDepositBound.getType());
        List<Integer> accountIds = AccountDepositBound.getAccountIds();
        if (accountIds.size() > 0) {
            List<AccountDepositBound> list = new ArrayList<>();
            accountIds.stream().forEach(accountId -> {
                list.add(new AccountDepositBound(AccountDepositBound.getSourceAccountId(), accountId, AccountDepositBound.getBoundTime(), AccountDepositBound.getType(), AccountDepositBound.getRemark()));
            });
            this.AccountDepositBoundDao.insertBatch(list);
        }
        return AccountDepositBound;
    }

    /**
     * 修改数据
     *
     * @param AccountDepositBound 实例对象
     * @return 实例对象
     */
    @Override
    public AccountDepositBound update(AccountDepositBound AccountDepositBound) {
        this.AccountDepositBoundDao.update(AccountDepositBound);
        List<AccountDepositBound> AccountDepositBounds = this.queryById(AccountDepositBound.getSourceAccountId(), AccountDepositBound.getType());
        return AccountDepositBounds.size() == 0 ? null : AccountDepositBounds.get(0);
    }

    /**
     * 通过主键删除数据
     *
     * @param sourceAccountId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer sourceAccountId, Integer type) {
        return this.AccountDepositBoundDao.deleteById(sourceAccountId, type) > 0;
    }
}